<template>
  <div>
    <el-container style="background: #f5f7f9;">
      <el-header style="background: #fff;padding: 12px 12px;height:auto;">
        <div>
          <el-form :model="queryData" :inline="true" size="small">
            <el-form-item>
              <el-select v-model="queryData.customerServiceStaff" filterable clearable placeholder="请选择客服">
                <el-option v-for="item in customerServiceStaffs" :key="item.userId" :value="item.userId" :label="item.userName" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="queryData.salesman" filterable clearable placeholder="请选择业务员">
                <el-option v-for="item in salesmans" :key="item.userId" :value="item.userId" :label="item.userName" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="queryData.orgOwner" filterable clearable placeholder="请选择客户">
                <el-option v-for="item in customerOptions" :key="item.orgOwnerName" :value="item.orgOwner" :label="item.orgOwnerName" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="queryData.enable" clearable placeholder="账号状态">
                <el-option value="true" label="启用" />
                <el-option value="false" label="停用" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="list">搜索</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-header>
      <el-main style="margin-top: 15px; background: #fff;padding: 12px 16px 0 16px;height: 100%;">
        <div style="height: 100%;display: flex;">
          <el-button v-has="30101" type="primary" size="small" @click="add">新增</el-button>
        </div>
        <el-table :data="table_body" stripe border :max-height="tableHeight">
          <el-table-column prop="orgOwnerName" label="客户" />
          <el-table-column prop="username" label="客户账号" />
          <el-table-column prop="name" label="联系人" />
          <el-table-column prop="orgOwnerCode" label="客户代码" />
          <el-table-column prop="credit" label="信用额度" />
          <el-table-column prop="createdAt" label="注册时间" />
          <el-table-column prop="salesmanName" label="业务员" />
          <el-table-column prop="customerServiceStaffName" label="客服" />
          <el-table-column prop="enable" label="状态">
            <template slot-scope="scope">
              <div v-if="scope.row.enable">启用</div>
              <div v-else>停用</div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-has="30102" size="small" shape="circle" type="text" @click="edit(scope.row)">编辑</el-button>
              <template v-has="30103">
                <el-button v-if="scope.row.enable" size="small" shape="circle" type="text" @click="disabled(scope.row)">停用</el-button>
                <el-button v-else size="small" shape="circle" type="text" @click="enable(scope.row)">启用</el-button>
              </template>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer style="padding:0;background: #fff;">
        <div style="text-align: right; padding-top: 10px;">
          <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50,100]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal" style="float:right;" @size-change="onPageSizeChange" @current-change="onPageChange" />
        </div>
      </el-footer>
    </el-container>
    <el-dialog :visible.sync="openModal" :title="drawerTitle" width="60%" top="0" :close-on-click-modal="false">
      <el-form :model="formData">
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="登录账号" label-position="top" required>
              <el-input v-model="formData.username" placeholder="登录账号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登录密码" label-position="top" required>
              <el-input v-model="formData.pwd" type="password" placeholder="登录密码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="联系人" label-position="top" required>
              <el-input v-model="formData.name" placeholder="联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" label-position="top" required>
              <el-input v-model="formData.companyTel" placeholder="联系方式" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="公司名称" label-position="top" required>
              <el-input v-model="formData.orgOwnerName" placeholder="公司名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司地址" label-position="top" required>
              <el-input v-model="formData.companyAddress" placeholder="公司地址" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="联系邮箱" label-position="top">
              <el-input v-model="formData.companyEmail" placeholder="联系邮箱" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="信用额度" label-position="top" required>
              <el-input v-model="formData.credit" type="number" placeholder="信用额度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="业务员" label-position="top">
              <el-select v-model="formData.salesman" placeholder="请选择业务员" style="width:100%;">
                <el-option v-for="item in salesmans" :key="item.userId" :value="item.userId" :label="item.userName" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客服" label-position="top">
              <el-select v-model="formData.customerServiceStaff" placeholder="请选择客服" style="width:100%;">
                <el-option v-for="item in customerServiceStaffs" :key="item.userId" :value="item.userId" :label="item.userName" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="可用渠道" label-position="top" required>
              <el-select v-model="formData.channelIds" placeholder="请选择产品" multiple clearable style="width:100%;">
                <el-option v-for="item in channelOptions" :key="item.id" :value="item.id" :label="item.name" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="重量佣金单价(KG)" label-position="top">
              <el-input v-model="formData.unitPriceCommission" type="number" placeholder="重量佣金单价(KG)" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="重量佣金科目" label-position="top">
              <el-select v-model="formData.chargeSubjectId" clearable filterable style="width:100%;" placeholder="如果没有可选项,请先新建费用科目">
                <el-option v-for="(item, index) in chargeSubjectOptions" :key="index" :value="item.id" :label="item.name" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" label-position="top">
              <el-input v-model="formData.remark" type="textarea" :rows="4" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button style="margin-right: 8px" @click="openModal = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from '@/utils/request'
import config from '@/settings'
import { mapState } from 'vuex'
import { copyProperty } from '@/utils/tools'
const BASE_URL = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro
export default {
  data() {
    return {
      baseUrl: BASE_URL + '/upload',
      openModal: false,
      table_body: [

      ],
      formDataDefault: {
        username: '',
        pwd: '',
        name: '',
        companyTel: '',
        orgOwner: '',
        orgOwnerName: '',
        companyEmail: '',
        companyAddress: '',
        credit: 0.00,
        licenseOssKey: '0',
        idCardFrontOssKey: '0',
        idCardBackOssKey: '0',
        salesman: '',
        customerServiceStaff: '',
        paymentTerm: 'DAILY_SETTLEMENT',
        needCustomerConfirm: 'false',
        channelIds: [],
        warehouseIds: [],
        unitPriceCommission: 0.00,
        chargeSubjectId: '',
        remark: ''
      },
      formData: {
        username: '',
        pwd: '',
        name: '',
        companyTel: '',
        orgOwner: '',
        orgOwnerName: '',
        companyEmail: '',
        companyAddress: '',
        credit: '',
        licenseOssKey: '0',
        idCardFrontOssKey: '0',
        idCardBackOssKey: '0',
        salesman: '',
        customerServiceStaff: '',
        paymentTerm: 'DAILY_SETTLEMENT',
        needCustomerConfirm: 'false',
        channelIds: [],
        warehouseIds: [],
        unitPriceCommission: '',
        chargeSubjectId: '',
        remark: ''
      },
      queryData: {
        pageSize: 50,
        currentPage: 0,
        orgOwner: '',
        enable: 'true'
      },
      pageTotal: 0,
      pageSizeOpts: [50, 100],
      drawerTitle: '新增',
      customerServiceStaffs: '',
      salesmans: '',
      warehouseOptions: [],
      chargeSubjectOptions: []
    }
  },
  computed: {
    tableHeight() {
      return document.documentElement.clientHeight - 290
    },
    pageSize() {
      return this.pageSizeOpts[0]
    },
    ...mapState(//
      {
        paymentTerm: state => state.base.paymentTerm,
        customerOptions: state => state.base.customerOptions,
        channelOptions: state => state.base.channelOptions
      }
    )
  },
  created() {
    this.$store.dispatch('getCustomerOptions')
    this.list()
    this.listSalesman()
    this.loadChargeSubjectOptions()
  },
  mounted() {
  },
  methods: {
    list() {
      request({
        url: 'customer/queryCustomer',
        data: this.queryData,
        method: 'post'
      }).then(res => {
        if (res.success) {
          this.loadedDate = true
          this.table_body = res.data.records
          this.pageTotal = res.data.total
        }
      })
    },
    listSalesman() {
      request({
        url: 'user/salesman-css',
        method: 'get'
      }).then(res => {
        if (res.success) {
          this.customerServiceStaffs = res.data.customerServiceStaffs
          this.salesmans = res.data.salesmans
        }
      })
    },
    submit() {
      request({
        url: 'customer/createAndUpdateCustomer',
        method: 'post',
        data: this.formData
      }).then(res => {
        if (res.success) {
          this.openModal = false
          this.list()
        }
      })
    },
    loadChanelOption(customerId) {
      request({
        url: 'customer/channel-option?customerId=' + customerId,
        method: 'get'
      }).then(res => {
        if (res.success) {
          this.formData.channelIds = res.data
        }
      })
    },
    edit(row) {
      this.drawerTitle = '编辑'
      copyProperty(this.formData, row)
      this.loadChanelOption(row.orgOwner)
      this.openModal = true
    },
    add() {
      for (const x in this.formData) {
        this.formData[x] = this.formDataDefault[x]
      }
      this.openModal = true
    },
    loadChargeSubjectOptions() {
      request({
        url: 'charge-subject/option',
        method: 'get'
      }).then(res => {
        if (res.success) {
          if (res.data) {
            this.chargeSubjectOptions = res.data
          }
        }
      })
    },
    disabled(row) {
      request({
        url: 'customer/disabled?orgOwner=' + row.orgOwner,
        method: 'GET'
      }).then(res => {
        if (res.success) {
          this.list()
        }
      })
    },
    enable(row) {
      request({
        url: 'customer/enable?orgOwner=' + row.orgOwner,
        method: 'GET'
      }).then(res => {
        if (res.success) {
          this.list()
        }
      })
    },
    onPageSizeChange(pageSize) {
      this.queryData.pageSize = pageSize
      this.list()
    },
    onPageChange(page) {
      this.queryData.currentPage = page
      this.list()
    }
  }
}
</script>
